package com.frame.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.frame.domain.Activiti;
import com.frame.domain.SysDictData;
import com.frame.domain.SysUser;
import com.frame.domain.SysUserLeaves;
import com.frame.service.ActivitiService;
import com.frame.service.SysUserService;
import com.frame.utils.Result;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/activiti")
public class ActivitiController {

    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    TaskService taskService;
    @Autowired
    HistoryService historyService;
    @Autowired
    ActivitiService activitiService;
    @Autowired
    HttpServletRequest request;

    @RequestMapping("/save")
    public Result save(@RequestBody Activiti activiti){
        String userName = (String) JWTUtil.parseToken(request.getHeader("token")).getPayload("role");
        activiti.setUserName(userName);
        activitiService.save(activiti);
        repositoryService.createDeployment()
                .addClasspathResource("bpmn/agricultural.bpmn20.xml")
                .deploy();
        Map<String, Object> var3=new HashMap<>();
        var3.put("days",activiti.getDays());
        runtimeService.startProcessInstanceByKey("agricultural",activiti.getId()+"",var3).getBusinessKey().trim();
        return Result.success();
    }


    @RequestMapping("/listAvticiti")
    public Result listAvticiti(){
        String token = request.getHeader("token");
        JWT of = JWT.of(token);
        String role = (String) of.getPayload("role");

        ArrayList<Activiti> activitis = new ArrayList<>();
        List<Task> list = taskService.createTaskQuery().taskAssignee(role).list();
        for (Task task : list) {
            HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
                    .processInstanceId(task.getProcessInstanceId())
                    .singleResult();
            String businessKey = historicProcessInstance.getBusinessKey();
            Activiti byId=activitiService.getById(businessKey);
            byId.setTaskId(task.getId());
            byId.setTaskName(task.getName());
            activitiService.updateById(byId);
            activitis.add(byId);
        }
        return Result.success(activitis);
    }


    @RequestMapping("/check")
    public Result check(String taskId){



        activitiService.UpdateTaskId1(taskId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("bohui",1);
        taskService.complete(taskId,map);
        return Result.success();
    }


    @RequestMapping("/bohui")
    public Result bohui(String taskId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("bohui",0);
        taskService.complete(taskId,map);
        return Result.success();
    }

    @RequestMapping("getLeads")
    public Result getLeads(){
        List<SysUser> list=activitiService.getLeads();
        return Result.success(list);
    }

    @RequestMapping("getManager")
    public Result getManager(){
        List<SysUser> list=activitiService.getManager();
        return Result.success(list);
    }

    @RequestMapping("getPresident")
    public Result getPresident(){
        List<SysUser> list=activitiService.getPresident();
        return Result.success(list);
    }

    @RequestMapping("getActiviti")
    public Result getLeave(Integer id){
        Activiti leave = activitiService.selectActivitiById(id);
        return Result.success(leave);
    }

    @Autowired
    SysUserService sysUserService;
    @RequestMapping("getName")
    public Result getName(){
        Object UserId = JWTUtil.parseToken(request.getHeader("token"));
        System.out.println(UserId);
        SysUser sysUser=sysUserService.selectUserById(UserId);
        return Result.success(sysUser.getUserName());
    }

    @GetMapping("myCompletion")
    public Result myCompletion(String status){
        String userName = (String) JWTUtil.parseToken(request.getHeader("token")).getPayload("role");
        List<SysUserLeaves> list=activitiService.myCompletion(userName,status);
        return Result.success(list);
    }


    @GetMapping("myCompletion")

    @RequestMapping("getLeaveType")
    public Result getActivitiType(){
        List<SysDictData> sysDictDataList=activitiService.getActivitiType();
        return Result.success(sysDictDataList);
    }

}